package net.telesing.tcmc.common;

import java.io.Serializable;

import org.nutz.dao.Cnd;
import org.nutz.dao.pager.Pager;
import org.nutz.lang.Strings;

/**
 * Created by 白义顺 on 2014/12/10.
 */
public class QueryParam implements Serializable
{
    public final static String ORDER_DESC = "desc";
    public final static String ORDER_ASC = "asc";

    private int page;//第几页
    private int rows;//显示行数
    private String sort;//排序字段
    private String order;//排序方式

    public int getPage()
    {
        return page;
    }

    public void setPage(int page)
    {
        this.page = page;
    }

    public int getRows()
    {
        return rows;
    }

    public void setRows(int rows)
    {
        this.rows = rows;
    }

    public String getSort()
    {
        return sort;
    }

    public void setSort(String sort)
    {
        this.sort = sort;
    }

    public String getOrder()
    {
        return order;
    }

    public void setOrder(String order)
    {
        this.order = order;
    }

    public Pager getPager()
    {
        Pager pager = new Pager();
        pager.setPageNumber(page);
        pager.setPageSize(rows);
        return pager;
    }

    public Cnd buildCnd()
    {
        return buildOrder(buildWhere());
    }

    public Cnd buildWhere()
    {
        return Cnd.where("1", "=", 1);
    }

    public Cnd buildOrder(Cnd cnd)
    {
        if (!Strings.isEmpty(sort))
        {
            if (ORDER_ASC.equals(order))
            {
                cnd.asc(sort);
            }
            else if (ORDER_DESC.equals(order))
            {
                cnd.desc(sort);
            }
        }
        return cnd;
    }

}
